<%@ page contentType= "text/html;charset=utf-8" %>

<html>
<%@ include file="../common/base.jsp" %>

<script type="text/javascript" src="../styles/date/WdatePicker.js">
</script>

<body>

<script language="javascript">
function onSearch(){
	document.forms[0].action = "./index.o";
	document.forms[0].submit();
}

function onReset(){
	if(document.getElementById("step"))
		document.getElementById("step").value="all";
	if(document.getElementById("finalSn"))
		document.getElementById("finalSn").value="";
	if(document.getElementById("internalSn"))
		document.getElementById("internalSn").value="";
	if(document.getElementById("pn"))
		document.getElementById("pn").value="";
	if(document.getElementById("keyword"))
		document.getElementById("keyword").value="";
	if(document.getElementById("operator"))
		document.getElementById("operator").value="all";
	if(document.getElementById("beginDateStr"))
		document.getElementById("beginDateStr").value="";
	if(document.getElementById("endDateStr"))
		document.getElementById("endDateStr").value="";
}

function onArchive(){
	window.location.href = "./archive.o";
}

function onUnArchive(){
	window.location.href = "./unarchive.o";
}
</script>

<div id="container">
<%@ include file="../common/header.jsp" %>

	<div id="menu">
	     	<a href="<c:url value="/common/home.o"/>"><fmt:message key="Home" /></a> > <fmt:message key="Task" />
	        <div  style="float: right" ><fmt:message key="Logged" />: <c:out value="${currentUser.username}" /> (<a href="<c:url value="/common/logout.o"/>"><fmt:message key="Logout" /></a>)</div>
	</div>
	<div id="midbody">
	     	<div id="leftside">
		 		<br>
				<shiro:hasPermission name="process:create">
				<a href="./start.o"><fmt:message key="CreateTask" /></a>
				<br><br>
				</shiro:hasPermission>
		   		<a href="<c:url value="/step/index.o"/>"><fmt:message key="AllTask" /></a>
				<br><br>
				<a href="./index.o?step=<%= StepActionUtil.TESTRESULT_FAILURE %>"><fmt:message key="FailTask" /></a>
				<br><br>
				<a href="./index.o?step=<%= StepActionUtil.TESTRESULT_WAIVE %>"><fmt:message key="WaiveTask" /></a>
				<shiro:hasPermission name="process:create">
				<br><br>
				<a href="./index.o?archive=true"><fmt:message key="ArchiveTask" /></a>
				</shiro:hasPermission>
		   	</div>
			<div id="rightside">
	
				<div id="bigbox">
				    <div class="title clearfix">
				    	<div style="float: left">
				    		<fmt:message key="Task" />
				    		<% if(request.getSession().getAttribute("searchCommandIndex") != null && ((SearchCommand)request.getSession().getAttribute("searchCommandIndex")).getArchive()){ %>
				    			- <fmt:message key="Archive" />
				    		<% } %>
				    	</div>
				    	<%@ include file="../common/hidemenu.jsp" %>
				    </div>
				    <div class="content">
				        <form:form modelAttribute="searchCommand">
				            <form:errors path="*" element="div" cssClass="errors"/>
				            <shiro:hasPermission name="process:delete">
				            <div>
				            	<span><fmt:message key="Status" />:</span><form:select path="step" items="${statuses}"/>&nbsp;&nbsp;
				            	<span><fmt:message key="Operator" />:</span><form:select path="operator" items="${users}" itemValue="username" itemLabel="username"/>&nbsp;&nbsp;
				            	<span><fmt:message key="FinalSN" />:</span><form:input path="finalSn" size="10"/>&nbsp;&nbsp;
				            	<span><fmt:message key="InternalSN" />:</span><form:input path="internalSn" size="10"/>&nbsp;&nbsp;
				            	<span>PN:</span><form:input path="pn" size="10"/>&nbsp;&nbsp;
				            </div>
				            <div>
				            	<span><fmt:message key="From" />:</span>
				            	<input type="text" id="beginDateStr" name="beginDateStr" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" class="Wdate" style="width:100px" value="<fmt:formatDate  value="${searchCommand.beginDate}" pattern="yyyy-MM-dd"/>">&nbsp;&nbsp;
				            	<span><fmt:message key="To" />:</span>
				            	<input type="text" id="endDateStr" name="endDateStr" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" class="Wdate" style="width:100px" value="<fmt:formatDate  value="${searchCommand.endDate}" pattern="yyyy-MM-dd"/>">&nbsp;&nbsp;
				            	<span><fmt:message key="Keyword" />:</span><form:input path="keyword"/>&nbsp;&nbsp;
				            	<input type="button" value="<fmt:message key='Search' />" onclick="onSearch();">
				            	<input type="button" value="<fmt:message key='Reset' />" onclick="onReset();">
				            </div>
				            </shiro:hasPermission>
				            <shiro:lacksPermission name="process:delete">
				            <div>
				            	<span><fmt:message key="InternalSN" />:</span><form:input path="internalSn" size="10"/>&nbsp;&nbsp;
				            	<span>PN:</span><form:input path="pn" size="10"/>&nbsp;&nbsp;
				            	<input type="button" value="<fmt:message key='Filter' />" onclick="onSearch();">
				            	<input type="button" value="<fmt:message key='Reset' />" onclick="onReset();">
				            </div>
				            </shiro:lacksPermission>
				        </form:form>
				        
						<c:if test="${not empty lockError}">
							<div style="color:red">
								<fmt:message key="error.product.locked">
									<fmt:param value="${lockError}" />
								</fmt:message>
							</div>
						</c:if>
						
						<table id="processQuery">
							<tr>
								<th>
									<fmt:message key="ID" />
								</th>
								<th>
									<fmt:message key="Lot" />
								</th>
								<th>
									<fmt:message key="Process" />
								</th>
								<th width="10%">
									<fmt:message key="InnerSN" />
								</td>
								<th width="10%">
									<fmt:message key="FinalSN" />
								</td>
								<th width="10%">
									PN
								</td>
								<th>
									<fmt:message key="Step" />
								</td>
								<th>
									<fmt:message key="LockedBy" />
								</td>
								<th>
									<fmt:message key="Actions" />
								</th>
							</tr>
							<% int i = 0; %>
							<c:forEach var="product" varStatus="status" items="${products}">
							<tr>
								<td>
									<c:out value="${product.id}" />
								</td>
								<td>
									<c:out value="${product.lot}" />
								</td>
								<td>
									<c:out value="${product.processId}" />
								</td>
								<td>
									<c:out value="${product.internalSn}" />
								</td>
								<td>
									<c:out value="${product.finalSn}" />
								</td>
								<td>
									<c:out value="${product.pn}" />
								</td>
								<td>
									<c:out value="${product.status}" />
								</td>
								<td>
									<c:out value="${product.lockedBy}" />
								</td>
								<td>
									<% if(request.getSession().getAttribute("searchCommandIndex") == null || !((SearchCommand)request.getSession().getAttribute("searchCommandIndex")).getArchive()){ %>
									<c:if test="${empty product.lockedBy || product.lockedBy == currentUser.username}">
									<a href="./goon.o?productId=<c:out value='${product.id}' />"><fmt:message key="Continue" /></a>&nbsp;
									</c:if>
									<c:if test="${empty product.lockedBy}">
									|&nbsp;
									<a href="./lock.o?productId=<c:out value='${product.id}' /><%=null==request.getParameter("pager.offset")?"":"&pager.offset="+request.getParameter("pager.offset")%>"><fmt:message key="Lock" /></a>&nbsp;
									</c:if>
									<c:if test="${not empty product.lockedBy}">
									<shiro:hasRole name="Admin">
									|&nbsp;
									<a href="./unlock.o?productId=<c:out value='${product.id}' /><%=null==request.getParameter("pager.offset")?"":"&pager.offset="+request.getParameter("pager.offset")%>"><fmt:message key="Unlock" /></a>&nbsp;
									</shiro:hasRole>
									<shiro:lacksRole name="Admin">
									<c:if test="${product.lockedBy == currentUser.username}">
									|&nbsp;
									<a href="./unlock.o?productId=<c:out value='${product.id}' /><%=null==request.getParameter("pager.offset")?"":"&pager.offset="+request.getParameter("pager.offset")%>"><fmt:message key="Unlock" /></a>&nbsp;
									</c:if>
									</shiro:lacksRole>
									</c:if>
									<shiro:hasPermission name="process:delete">
									|&nbsp;
									<a href="./delete.o?productId=<c:out value='${product.id}' />"><fmt:message key="Delete" /></a>
									</shiro:hasPermission>
									<% } %>
								</td>
							</tr>
							</c:forEach>
						</table>
						<br>
						<% com.octopus.optiworks.controller.SearchCommand command = (com.octopus.optiworks.controller.SearchCommand)request.getAttribute("searchCommand"); %>
						<pg:pager url="indexpage.o" items="<%= command.getTotal()%>" export="currentPageNumber=pageNumber" maxIndexPages="<%= command.getPageIndex()%>" maxPageItems="<%= command.getLength()%>">  
						    <pg:first>  
						        <a href="<c:out value="${pageUrl}" />">[<fmt:message key="First" />]</a>  
						    </pg:first>  
						    <pg:prev>  
						        <a href="<c:out value="${pageUrl}" />">[<fmt:message key="Previous" />]</a>  
						    </pg:prev>  
						    <pg:pages>  
						        <c:choose>  
						            <c:when test="${currentPageNumber eq pageNumber}">  
						                <font color="red">[<c:out value="${pageNumber}" />]</font>  
						            </c:when>  
						            <c:otherwise>  
						                <a href="<c:out value="${pageUrl}" />">[<c:out value="${pageNumber}" />]</a>  
						            </c:otherwise>  
						        </c:choose>  
						    </pg:pages>  
						    <pg:next>  
						        <a href="<c:out value="${pageUrl}" />">[<fmt:message key="Next" />]</a>  
						    </pg:next>  
						    <pg:last>  
						        <a href="<c:out value="${pageUrl}" />">[<fmt:message key="Last" />]</a>  
						        -
						        [<fmt:message key="Total" />:<%= null==command?0:command.getTotal()%>]
						    </pg:last>  
						</pg:pager>
						<br>
						<br>
						<shiro:hasPermission name="process:archive">
			    		<% if(request.getSession().getAttribute("searchCommandIndex") != null && ((SearchCommand)request.getSession().getAttribute("searchCommandIndex")).getArchive()){ %>
			    			<input type="button" value="<fmt:message key='UnArchive' />" onclick="onUnArchive();">
			    		<% }else{ %>
			    			<input type="button" value="<fmt:message key='Archive' />" onclick="onArchive();">
			    		<% }%>
			    		</shiro:hasPermission>
					</div>
				</div>
			</div>
	</div>
	<%@ include file="../common/foot.jsp" %>
</div>
</body>
</html>
